package com.labManage.service.impl;

import com.labManage.dao.BorrowInfoDao;
import com.labManage.dao.GoodsDao;
import com.labManage.dao.KindsDao;
import com.labManage.dao.UserDao;
import com.labManage.entity.BorrowInfo;
import com.labManage.entity.Goods;
import com.labManage.entity.User;
import com.labManage.service.BorrowService;
import com.labManage.util.BorrowStateUtil;
import com.labManage.vo.BorrowHandleState;
import com.labManage.vo.BorrowInfoVo;
import com.labManage.vo.GoodsHandleState;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class BorrowServiceImpl implements BorrowService {

    @Autowired
    BorrowInfoDao borrowInfoDao;
    @Autowired
    UserDao userDao;
    @Autowired
    GoodsDao goodsDao;
    @Autowired
    KindsDao kindsDao;
    @Override
    public List<BorrowInfoVo> getBorrowInfo(Integer page, Integer limit,String borrowPersonNo) {
        Long borrowPersonId = null;
        int startIndex = (page-1)*limit;
        if(borrowPersonNo!=""){
            User user = userDao.selectByUserNo(borrowPersonNo);
            if(user!=null){
                borrowPersonId = user.getId();
            }
        }
        List<BorrowInfo> borrowInfos = borrowInfoDao.selectBorrowInfoLimit(startIndex, limit,borrowPersonId);
        ArrayList<BorrowInfoVo> borrowInfoVos = new ArrayList<>();
        if(null!=borrowInfos){
            for (BorrowInfo borrowInfo:borrowInfos) {
                BorrowInfoVo borrowInfoVo = new BorrowInfoVo();
                String borrowUser = userDao.selectUserNameById(borrowInfo.getBorrowPersonId());
                String borrowGoods = goodsDao.selectGoodsNameById(borrowInfo.getBorrowGoodsId());
                borrowInfoVo.setId(borrowInfo.getId());
                borrowInfoVo.setBorrowPerson(borrowUser);
                borrowInfoVo.setBorrowGoods(borrowGoods);
                borrowInfoVo.setBorrowTime(borrowInfo.getBorrowTime());
                borrowInfoVo.setReturnTime(borrowInfo.getReturnTime());
                borrowInfoVo.setBorrowHandleState(borrowInfo.getBorrowHandleState());
                borrowInfoVo.setCreatePerson(borrowInfo.getCreatePerson());
                borrowInfoVo.setCreateTime(borrowInfo.getCreateTime());
                borrowInfoVo.setModifyPerson(borrowInfo.getModifyPerson());
                borrowInfoVo.setModifyTime(borrowInfo.getModifyTime());
                borrowInfoVo.setBorrowNum(borrowInfo.getBorrowNum());
                //1、按钮使能
                Boolean buttonEnable = BorrowStateUtil.getBorrowButtonEnable(borrowInfo.getBorrowHandleState());
                borrowInfoVo.setButtonEnable(buttonEnable);
                //2、按钮字
                String word = BorrowStateUtil.getBorrowButtonWord(borrowInfo.getBorrowHandleState());
                if(null!=word){
                    borrowInfoVo.setBorrowButtonWord(word);
                }else{
                    return null;
                }
                borrowInfoVos.add(borrowInfoVo);
            }
            return borrowInfoVos;
        }else{
            return null;
        }
    }

    @Override
    public int borrowHandleState(Long borrowId) {
        //1、获取借阅信息
        BorrowInfo selectBorrowInfo = borrowInfoDao.selectBorrowInfoById(borrowId);
        if(null!=selectBorrowInfo){
            //2、获取当前借阅信息的借阅操作状态
            String nowBorrowHandleState = selectBorrowInfo.getBorrowHandleState();
            //3、根据当前借阅操作状态获取待更新借阅操作状态
            Long goodsId = selectBorrowInfo.getBorrowGoodsId();
            Goods selectGoods = goodsDao.selectGoodsById(goodsId);
            if(null!=selectGoods){
                Long kindId = selectGoods.getKindsId();
                Boolean needReback = kindsDao.selectKindsNeedRebackByKindsId(kindId);
                String newBorrowHandleState = BorrowStateUtil.getBorrowHandleStateChanged(nowBorrowHandleState,needReback);
                if(null!=newBorrowHandleState){
                    //4、更新借阅操作状态
                    BorrowInfo updateBorrowInfo = new BorrowInfo();
                    Subject subject = SecurityUtils.getSubject();
                    User loginUser = (User)subject.getPrincipal();
                    updateBorrowInfo.setModifyPerson(loginUser.getUsername());
                    updateBorrowInfo.setModifyTime(new Date());
                    updateBorrowInfo.setBorrowHandleState(newBorrowHandleState);
                    updateBorrowInfo.setId(borrowId);
                    if((newBorrowHandleState.equals(BorrowHandleState.AGREED_BORROW_APPLY)||(newBorrowHandleState.equals(BorrowHandleState.NOT_NEED_REBACK)))){
                        updateBorrowInfo.setBorrowTime(new Date());
                    }else if(newBorrowHandleState.equals(BorrowHandleState.AGREED_REBACK_APPLY)){
                        updateBorrowInfo.setReturnTime(new Date());
                    }
                    int updateResult = borrowInfoDao.updateBorrowInfo(updateBorrowInfo);
                    if(0<updateResult){
                        //5、获取该物品当前操作状态
                        String nowGoodsHandleState = selectGoods.getGoodsHandleState();
                        //6、根据当前物品操作状态获取待更新物品操作状态
                        String newGoodsHandleState = BorrowStateUtil.getGoodsHandleStateChanged(nowBorrowHandleState,needReback);
                        //7、根据当前借阅操作状态获取物品状态
                        String newGoodsState = BorrowStateUtil.getGoodsState(nowBorrowHandleState,needReback);
                        if((null!=newGoodsHandleState)&&(null!=newGoodsState)){
                            //7、更新物品操作状态与物品状态
                            Goods updateGoods = new Goods();
                            updateGoods.setId(selectGoods.getId());
                            updateGoods.setModifyPerson(loginUser.getUsername());
                            updateGoods.setModifyTime(new Date());
                            updateGoods.setGoodsHandleState(newGoodsHandleState);
                            updateGoods.setGoodsState(newGoodsState);
                            if((needReback==true)&&(newGoodsState.trim().equals("在库"))){
                                updateGoods.setGoodsNum((long) 1);
                            }
                            if((needReback==true)&&(newGoodsState.trim().equals("不在库"))){
                                updateGoods.setGoodsNum((long) 0);
                            }
                            if(needReback==false){
                                //获取该物品现存总数量
                                Long nowGoodsNum = goodsDao.selectGoodsById(goodsId).getGoodsNum();
                                Long Sub = nowGoodsNum-selectBorrowInfo.getBorrowNum();
                                if(Sub<0){
                                    BorrowInfo updateBorrowInfo1 = new BorrowInfo();
                                    updateBorrowInfo1.setId(borrowId);
                                    updateBorrowInfo1.setBorrowTime(new Date());
                                    updateBorrowInfo1.setModifyTime(new Date());
                                    updateBorrowInfo1.setModifyPerson(loginUser.getUsername());
                                    updateBorrowInfo1.setBorrowHandleState(BorrowHandleState.NUM_NOT_ENOUGH);
                                    int updateResult1 = borrowInfoDao.updateBorrowInfo(updateBorrowInfo1);
                                    if(updateResult1>0){
                                        //库存不足
                                        return 4;
                                    }else {
                                        return 3;
                                    }
                                }else {
                                    updateGoods.setGoodsNum(Sub);
                                }
                            }
                            int updateGoodsResult = goodsDao.updateGoods(updateGoods);
                            if(updateGoodsResult>0){
                               return 0;
                            }else{
                                //服务器异常
                                return 3;
                            }
                        }else{
                            //非法访问
                            return 2;
                        }
                    }else{
                        //服务器异常
                        return 3;
                    }
                }else{
                    //非法访问
                    return 2;
                }

            }else{
                //服务器异常
                return 3;
            }

        }else{
            //借阅信息不存在
            return 1;
        }

    }

    @Override
    public Long getBorrowInfoNum(String borrowPersonNo) {
        Long borrowPersonId = null;
        if(borrowPersonNo!=""){
            User user = userDao.selectByUserNo(borrowPersonNo);
            if(user!=null){
                borrowPersonId = user.getId();
            }
        }
        return borrowInfoDao.selectBorrowInfoNum(borrowPersonId);

    }
}
